package com.shanjupay.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.api.AppService;
import com.shanjupay.merchant.api.dto.AppDTO;
import com.shanjupay.merchant.convert.AppConvert;
import com.shanjupay.merchant.entity.App;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.AppMapper;
import com.shanjupay.merchant.mapper.MerchantMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 *
 *
 *
 *
 */
@Slf4j
@Service
public class AppServiceImpl implements AppService  {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     *  创建应用
     * @param merchantId
     * @param app
     * @return
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO app) throws BusinessException {

        // 参数校验
        if(merchantId == null || app == null || app.getAppName() == null ){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        // 校验商户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if(merchant == null ){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        // 取出商户资质申请状态
        String auditStatus = merchant.getAuditStatus();
        if(!"2".equals(auditStatus)){
            throw new BusinessException(CommonErrorCode.E_200003);
        }

        // 应用名称需要校验唯一性
        // 传入的应用名称
        String appName = app.getAppName();
        Boolean existAppName = isExistAppName(appName);
        if(existAppName){
            throw new BusinessException(CommonErrorCode.E_200004);
        }

        // 生成应用的appid
        String appId = UUID.randomUUID().toString();
        app.setAppId(appId);
        app.setMerchantId(merchantId);

        App entity = AppConvert.INSTANCE.dto2entity(app);

        // 插入数据库
        int insert = appMapper.insert(entity);

        log.info("插入应用的数据库返回结果：" + insert);

        return AppConvert.INSTANCE.entity2dto(entity);
    }

    /**
     * 根据商户id 查询应用
     *
     * @param merchantId
     * @return
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        if(merchantId  == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        List<App> apps = appMapper.selectList(new QueryWrapper<App>().lambda().eq(App::getMerchantId, merchantId));
        List<AppDTO> appDTOS = AppConvert.INSTANCE.listentity2dto(apps);
        return appDTOS;
    }

    /**
     * 根据业务id 查询应用
     *
     * @param appId
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppById(String appId) throws BusinessException {
        if(appId  == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        AppDTO appDTO = AppConvert.INSTANCE.entity2dto(app);
        return appDTO;
    }

    /**
     *
     *  校验应用名是否已被使用
     *
     * @param name
     * @return
     */
    public Boolean isExistAppName(String name){
        Integer count = appMapper.selectCount(new QueryWrapper<App>().lambda().eq(App::getAppName,name));
        return count.intValue() > 0 ;
    }


}
